package com.ruoyi.web.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ruoyi.core.config.GlobalProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan(basePackages = {"com.ruoyi.web.mapper", "com.ruoyi.modules.**.mapper"})
public class MybatisConfig {

    @Autowired
    private GlobalProperties globalProperties;

    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        //自动逸填充插件
        interceptor.addInnerInterceptor(new DefaultMetaObjectInterceptor());


        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.getDbType(globalProperties.getDb().getDbType().getValue())));//如果配置多个插件,切记分页最后添加
        //interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
        return interceptor;
    }
}
